package com.zbdx.controller;

import com.google.gson.Gson;
import com.zbdx.domain.Order;
import com.zbdx.service.IOrderServcie;
import com.zbdx.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * TODO
 *
 * @author shengyuee
 * @version 1.0
 * 2024/9/10 16:30
 */
@WebServlet("/orderServlet")
public class OrderServlet extends HttpServlet {
    private IOrderServcie orderServcie = new OrderServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf8");

        String method = req.getParameter("method");
        System.out.println("method = " + method);
        if("findAll".equals(method)){
            try {
                this.findAll(req,resp);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }else if("findById".equals(method)){
            try {
                this.findById(req,resp);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } else if("removeById".equals(method)){
            try {
                this.removeById(req,resp);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }else{
            System.out.println("请求地址错误");
        }

    }

    /**
     * 删除用户
     * @param req
     * @param resp
     */
    private void removeById(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String orderId = req.getParameter("orderId");
        System.out.println("orderId = " + orderId);
        if(orderId != null){
            // 将字符串userid类型转换成整数
            int uid = Integer.parseInt(orderId);
            // 调用业务层的根据Id的删除方法
            boolean flag = orderServcie.removeOrderById(uid);
            PrintWriter writer = resp.getWriter();
            if(flag){
                writer.print(1);
            }else{
                writer.print(0);
            }
        }else{
            System.out.println("商品ID为空");
        }
    }

    /**
     * 根据用户ID查询用户信息
     * @param req
     * @param resp
     */
    private void findById(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        String orderId = req.getParameter("orderId");
        System.out.println("orderId = " + orderId);
        if(orderId != null){
            // 将字符串转换成int类型
            int uid = Integer.parseInt(orderId);
            // 调用业务层根据ID查询用户的方法
            Order order =orderServcie.findById(uid);
            // 获取session
            HttpSession session = req.getSession();
            // 将user对象放到会话session域共享
            session.setAttribute("order", order);
            //转发到查找页面
            req.getRequestDispatcher("page/findOneOrder.jsp").forward(req,resp);


        }
    }
    private void addOrder(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String data = req.getParameter("data");
        System.out.println("data = " + data);
        Gson gson = new Gson();
        // 将JSON字符串转换成对应的User对象
        Order order = gson.fromJson(data, Order.class);
        System.out.println("order = " + order);
        boolean flag = orderServcie.addOrder(order);
        System.out.println("flag = " + flag);
        PrintWriter writer = resp.getWriter();
        if(flag){
            writer.print(1);
        }else{
            writer.print(0);
        }
        writer.close();
    }
    /**
     * 查询所有
     * @param req
     * @param resp
     */
    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        List<Order> list = orderServcie.findAll();
        // 使用HashMap存储数据
        HashMap<String,Object> map = new LinkedHashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",100);
        map.put("data",list);
        System.out.println(map);
        // 将map集合对象转换成JSON字符串
        Gson gson = new Gson();
        String json = gson.toJson(map);
        System.out.println("json = " + json);
        PrintWriter writer = resp.getWriter();
        writer.print(json);
        writer.close();

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
